package com.tjp.oss.adapter;

import com.tjp.oss.entity.FileInfo;
import com.tjp.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @program: jz-club
 * @description: aliyun服务接口实现类
 * @author: aaronTang
 * @create: 2024-03-07
 */
public class AliStorangeAdapter implements StorangeAdapter {

    /**
     * SneakyThrows允许你在不手动声明throws异常的情况下，编写可能抛出受检查异常的代码,使代码更简介
     * 存在的问题:通过隐藏异常的声明，可能会使得错误处理变得不那么明显,不利于问题处理和定位
     */

    @Resource
    private MinioUtil minioUtil;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public List<String> getAllButket() {
        return minioUtil.getAllButket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        //创建桶
        minioUtil.createBucket(bucket);
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + uploadFile.getName());
        }else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getName());
        }


    }

    @Override
    @SneakyThrows
    public InputStream downloadFile(String bucketName, String objectName) {
        return minioUtil.downloadFile(bucketName, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucketObject(String bucketName, String objectName) {
        minioUtil.deleteBucketObject(bucketName, objectName);
    }
}
